<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\Admin;

use App\Controller\AppBaseController;
use App\Model\Party;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\AdminApiAuth;

#[Controller(prefix: 'admin')]
#[Middleware(AdminApiAuth::class)]

class PartyController extends AppBaseController
{
    /**
     * 后台--支部列表
     * @return object
     */
    #[RequestMapping(path:'partyList',methods: 'get')]
    public function partyList(): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $list = Party::selectRaw('id,name')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();

            $total = 0;
            $next = 0;
            if($list->isNotEmpty()){
                if($page==1) $total = Party::count();
                if(count($list)>$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list,'next' => $next,'total'=>$total]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 后台--支部详情
     * @return object
     */
    #[RequestMapping(path:'partyInfo',methods: 'get')]
    public function partyInfo(): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            //指定支部
            if($_admin->level==1){
                $party_id = $this->request->input('party_id',null);
                if($party_id==null || !is_numeric($party_id)) return $this->reMsg->re(10007);
            }else{
                $party_id = $_admin->party_id;
            }

            $info = Party::where('id',$party_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

    /**
     * 支部信息更新
     * @return object
     */
    #[RequestMapping(path:'upPartyInfo',methods: 'post,get')]
    public function upPartyInfo(): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            $data = $this->request->all();

            //虚拟数据
//            if(empty($data)){
//                $data['party_secretary'] = '张小奇';
//                $data['party_committee'] = '张小奇';
//                $data['party_number'] = 50;
//                $data['party_public_number'] = 100;
//                $data['transition_time'] = '2024-10-01 10:00:00';
//                $data['content'] = '正式的修改';
//                $data['images'] = ['/fqxinyi23/test/xyMessage1689216632369.png'];
//            }
            //虚拟数据

            if( empty($data['party_secretary'])
                || empty($data['party_committee'])
                || empty($data['transition_time'])
                || empty($data['content'])
                || empty($data['images'])
                || empty($data['party_id'])
            ){
                return $this->reMsg->re(10007);
            }
            //处理默认值
            if(empty($data['party_number'])) $data['party_number']=0;
            if(empty($data['party_public_number'])) $data['party_number']=0;

            //指定支部
            if($_admin->level==1){
                //超级管理员
                $party_id = $data['party_id'];
            }else{
                if($data['party_id']!=$_admin->party_id) return $this->reMsg->re(10009);
                $party_id = $data['party_id'];
            }

            $info = Party::where('id',$party_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);

            //是否是超级管理员 或者 所属自己的党支部
            if($_admin->level!=1 && $_admin->party_id!=$info->id) return $this->reMsg->re(10009);
            $info->party_secretary = $data['party_secretary'];
            $info->party_committee = $data['party_committee'];
            $info->party_number = $data['party_number'];
            $info->party_public_number = $data['party_public_number'];
            $info->transition_time = $data['transition_time'];
            $info->content = $data['content'];
            $info->images = $data['images'];
            $info->save();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

}